package com.example.demo.controller.rest;

import org.flowable.engine.ManagementService;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.IdmIdentityService;
import org.flowable.idm.api.User;
import org.flowable.ui.common.model.GroupRepresentation;
import org.flowable.ui.common.model.ResultListDataRepresentation;
import org.flowable.ui.common.model.UserRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xxx
 */
@RestController
@RequestMapping("/app")
public class EditorUsersResource {

    @Autowired
    protected IdmIdentityService idmIdentityService;
    @Autowired
    protected ManagementService managementService;

    @RequestMapping(value = "/rest/authenticate", method = RequestMethod.GET)
    public String authenticate() {
        return "{\"code\":\"200\"}";
    }

    @GetMapping(value = "/rest/account", produces = "application/json")
    public UserRepresentation account() {
        UserRepresentation user = new UserRepresentation();
        user.setId("admin");
        user.setFirstName("admin");
        user.setLastName("Administrator");
        user.setFullName("Test Administrator");
        user.setEmail("admin@flowable.org");
        user.getPrivileges().add("access-idm");
        user.getPrivileges().add("access-rest-api");
        user.getPrivileges().add("access-task");
        user.getPrivileges().add("access-modeler");
        user.getPrivileges().add("access-admin");
        // 参考官方提供的响应数据
        //return "{\"id\":\"admin\",\"firstName\":\"admin\",\"lastName\":\"Administrator\",\"email\":\"admin@flowable.org\",\"fullName\":\"Test Administrator\"
        // ,\"groups\":[],\"privileges\":[\"access-idm\",\"access-rest-api\",\"access-task\",\"access-modeler\",\"access-admin\"]}";
        return user;
    }

    @RequestMapping(value = "/rest/admin/users", method = RequestMethod.GET)
    public String users() {
        return "{\"code\":\"200\"}";
    }


    @RequestMapping(value = "/rest/editor-groups", method = RequestMethod.GET)
    public ResultListDataRepresentation getGroups(@RequestParam(required = false, value = "filter") String filter) {
        if (!StringUtils.isEmpty(filter)) {
            filter = filter.trim();
            String sql = "select * from act_id_group where NAME_ like #{name} limit 10";
            filter = "%" + filter + "%";
            List<Group> groups = idmIdentityService.createNativeGroupQuery().sql(sql).parameter("name", filter).list();
            List<GroupRepresentation> result = new ArrayList<>();
            for (Group group : groups) {
                result.add(new GroupRepresentation(group));
            }
            return new ResultListDataRepresentation(result);
        }
        return null;
    }

    @RequestMapping(value = "/rest/editor-users", method = RequestMethod.GET)
    public ResultListDataRepresentation getUsers(@RequestParam(value = "filter", required = false) String filter) {
        if (!StringUtils.isEmpty(filter)) {
            filter = filter.trim();
            String sql = "select * from act_id_user where ID_ like #{id} or LAST_ like #{name} limit 10";
            filter = "%" + filter + "%";
            List<User> matchingUsers = idmIdentityService.createNativeUserQuery().sql(sql).parameter("id", filter).parameter("name", filter).list();
            List<UserRepresentation> userRepresentations = new ArrayList<>(matchingUsers.size());
            for (User user : matchingUsers) {
                userRepresentations.add(new UserRepresentation(user));
            }
            return new ResultListDataRepresentation(userRepresentations);
        }
        return null;
    }
}
